#!/usr/bin/env python
"""
implementation of atom publishing protocol
Atom Publishing Protocol
http://www.ibm.com/developerworks/library/x-atompp1/
"""
__author__ = 'rx.wen218@gmail.com'

import httplib
import base64

class AtomPublishingProtocol:
    def AppendCredential(self, con):
        """
        append credential to http connection header
        basic http authentication
        the username and password is encoded with base64

        this method internally call endheaders, so place after
        all other http headers have been added
        """
        credential = base64.b64encode(self.user_name+':'+self.password)
        con.putheader('Authorization', 'Basic ' + credential)
        con.endheaders()

    def __init__(self, url, user_name, password):
        self.url = url
        self.user_name = user_name
        self.password = password
        con = httplib.HTTPConnection(url)
        con.putrequest('GET', '/user/app.php')
        self.AppendCredential(con)
        con.send('')
        response = con.getresponse()
        response_data = response.read()
        self.user_id = "5952093"
        pass

    def GetPosts(self):
        con = httplib.HTTPConnection(self.url)
        con.putrequest('GET', '/user/app.php/%s/posts'%self.user_id)
        self.AppendCredential(con)
        # con.putrequest('GET', '/collection/introspection')
        con.send('')
        response = con.getresponse()
        response_data = response.read()
        print response_data

    def AddPost(self, post):
        text = """<?xml version="1.0" ?>
<entry xmlns="http://www.w3.org/2005/Atom">
  <title>Atom-Powered Robots Run Amok</title>
  <link href="http://example.org/2003/12/13/atom03"/>
  <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  <updated>2003-12-13T18:30:02Z</updated>
  <author><name>James</name></author>
  <summary>Some text.</summary>
</entry>"""        
        con = httplib.HTTP(self.url)
        #con.putrequest('POST', '/user/app.php/%s/posts'%self.user_id)

        con.putrequest('POST', '/user/app.php/%s/posts/%s/posts'%(self.user_id, self.user_id))
        con.putheader('Content-Type', 'application/atom+xml; type=entry')
        con.putheader('Content-Length', str(len(text)))
        self.AppendCredential(con)
        con.send(text)
        statuscode, response, header = con.getreply()
        print statuscode  
        print response

    def EditPost(self, post):
        text = """<?xml version="1.0" ?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xml:lang="zh-cn">
        <id>http://rxwen.blogbus.com/logs/86153157.html</id>
        <title type="text">aaaaaa</title>
        <published>2010-06-11T13:35:06+08:00</published>
        <app:control>
                <app:draft>no</app:draft>
        </app:control>
        <author>
                <name></name>
        </author>
        <link href="http://rxwen.blogbus.com/logs/65727396.html" />
        <link rel="edit" href="http://www.blogbus.com/user/app.php/5952093/posts/5952093/post/86153157" />
        <content type="xhtml"><![CDATA[<p>aaaaaaaaaaaaaa</p>
<p>hello</p>]]></content>
        <summary type="text"><![CDATA[ldksfjlskdfjsldkfjsdjfk

hello

]]></summary>
</entry>
"""
        con = httplib.HTTP(self.url)
        con.putrequest('POST', '/user/app.php/5952093/posts/5952093/post/86153157')
        # basic http authentication
        credential = base64.b64encode(self.user_name+':'+self.password)
        con.putheader('Authorization', 'Basic ' + credential)
        con.putheader('Content-Type', 'application/atom+xml; type=entry')
        con.putheader('Content-Length', str(len(text)))
        con.endheaders()
        con.send(text)
        statuscode, response, header = con.getreply()
        #print statuscode + ' ' + response
        #response_data = response.read()

